package org.example.dao;

import org.example.utils.JDBCUtils;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

// 点赞 转发 收藏
public class FunctionsDao {

    // 点赞功能 1.判断是否点赞过 2.点赞
    //判断用户是否已经点赞
    public List<Integer> likePublish(int userId,int publishId) throws Exception{
        //		获得数据库链接
        Connection connection = JDBCUtils.getConnection();
//        String sql = "insert into users (username,password) values(?,?)";
        String sql = "select likes,collect,retransmission from functionc where user_id=? and publish_id=?";
//		使用PreparedStatement执行SQL语句可以防止SQL注入
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ps.setInt(2,publishId);
        ResultSet rs = ps.executeQuery();
        List<Integer> ls =new ArrayList<>();
        if(!rs.next()){
            // rs 为空时
//            System.out.println("用户未点赞过");
        }else {
            ls.add(rs.getInt("likes"));
//            System.out.println(ls);
            ls.add(rs.getInt("collect"));
            ls.add(rs.getInt("retransmission"));
//        System.out.println(ls+"??");
        }
        return ls;
    }

    // 查询中 点赞数量
    public List<Integer> counts(int publishId) throws Exception {
        //		获得数据库链接
        Connection connection = JDBCUtils.getConnection();
//        String sql = "insert into users (username,password) values(?,?)";
        String sql = "select count(likes),count(collect),sum(retransmission) from functionc where publish_id=?";
//		使用PreparedStatement执行SQL语句可以防止SQL注入
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,publishId);
        ResultSet rs = ps.executeQuery();
        rs.next();
        List<Integer> ls =new ArrayList<>();
        ls.add(rs.getInt(1));
        ls.add(rs.getInt(2));
        ls.add(rs.getInt(3));
//        System.out.println(ls);
        return ls;
    }

    // 点赞功能
    public boolean userLike(int userId,int publishId,boolean flag) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        //		判断用户是否已经点赞
        String sql;
        if(flag){  //点赞
            sql = "update functionc set likes=1 where user_id=? and publish_id=?";
        }else{ //取消点赞
            sql = "update functionc set likes=NULL where user_id=? and publish_id=?";
        }
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ps.setInt(2,publishId);
        ps.executeUpdate();
        return flag;
   }

   // 收藏 功能
   public boolean userCollect(int userId,int publishId,boolean flag) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql;
        if(flag){  //收藏
            sql = "update functionc set collect=1 where user_id=? and publish_id=?";
        }else{ //取消收藏
            sql = "update functionc set collect=NULL where user_id=? and publish_id=?";
        }
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ps.setInt(2,publishId);
        ps.executeUpdate();
        return flag;
   }

   // 用户转发 ,+1
   public boolean userRetransmission(int userId,int publishId) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql="update functionc set retransmission=retransmission+1 where user_id=? and publish_id=?";
       PreparedStatement ps = connection.prepareStatement(sql);
       ps.setInt(1,userId);
       ps.setInt(2,publishId);
       ps.executeUpdate();
//       rs.next();
       return true;
   }

   // 创建 功能区
    public void insertFunctions(int userId,int publishId) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into functionc (publish_id,user_id,create_time,update_time) values(?,?,now(),now())";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,publishId);
        ps.setInt(2,userId);
        ps.executeUpdate();
}

    // 统计 用户点赞 的数量
    public int countLike(int userId) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(likes) from functionc where user_id=? and likes=1";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getInt(1);
    }

    // 统计 用户收藏 的数量
    public int countCollect(int userId) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(collect) from functionc where user_id=? and collect=1";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getInt(1);
    }

    // 统计 用户转发 的数量
    public int countRetransmission(int userId) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql = "select sum(retransmission) from functionc where user_id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getInt(1);
    }


}

